
opendir $dir, $ARGV[0] or die "can not open the directory $ARGV[0]!\n";

@samples[2000];
@timestamp[2000];
$fileNum = 0;
$sampleNum = 0;

#get the minimum start time of 16 channels.
while ($file = readdir $dir) {
	$file_path = "$ARGV[0]\\$file";
	print ("$file_path  \n $file\n");
	
	$logfile = "\.log";
	#get input samples
	if($file =~ /$logfile/) {
		open fin, "<$file_path" or die "can not open file $file_path!\n";
		$fileNum++;
		
		$startTime = 0;
		$endTime = 0;
		$period = 0;
		$round = -1;
		open fout, ">$ARGV[0]\\addTime\\$file" or die "can not open file $dir\\addTime\\01$file!\n";
		open fout01, ">$ARGV[0]\\addTime\\01$file" or die "can not open file $dir\\addTime\\01$file!\n";
		while (<fin>) {
			chomp;
			@info = split/,/;
			
			
			if($info[0] eq "infoc") {
				next;
			}
			
			if($info[0] eq "infos"){
				$startTime = $info[2];
				$round = $info[1];
			}
			else {
				if($info[0] eq "infoe" && ($round+1) == $info[1]){
					$endTime = $info[2];
					$period = $endTime-$startTime;
					for($i=0;$i<1000;$i++){
						$timestamp[$i] = $startTime + ($i+1)*$period/1000;
						print fout "$timestamp[$i] $samples[$i]\n";
						if($samples[$i]>=-85){
							print fout01 "$timestamp[$i] 1\n";
							print "$timestamp[$i] $samples[$i] 1\n";
						}
						if($samples[$i]<-85){
							print fout01 "$timestamp[$i] 0\n";
						}
						
					}
					for($i=0;$i<1000;$i++){
						$timestamp[$i] = 0;
						$samples[$i] = 0;
					}
					$startTime = 0;
					$endTime = 0;
					$period = 0;
					$sampleNum = 0;
				}
				else {
					# if($info[0]>=127) {
						# $samples[$sampleNum++] = int($info[0] - 256 -45);
					# }
					# else {
						# $samples[$sampleNum++] = int($info[0] -45);
					# }
					$samples[$sampleNum++] = $info[0];
				}
			}
		}
		close fin;
		close fout;
		close fout01;
	}
	else {
		print("No log files found!\n");
		next;
	}
	
}

